package com.rick.awarenessdemo

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.WindowManager
import com.amap.api.services.core.PoiItem
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ServiceUtils
import com.blankj.utilcode.util.ThreadUtils
import com.blankj.utilcode.util.ToastUtils
import com.rick.awarenessdemo.base.BaseActivity
import com.rick.awarenessdemo.databinding.ActivityMainBinding
import com.rick.awarenessdemo.util.AwarenessUtil
import com.rick.awarenessdemo.util.PermissionUtil

class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {

    companion object {
        private const val REQUEST_CODE_PERMISSION = 1
        private const val REQUEST_CODE_APPLICATION_DETAILS_SETTINGS = 2
        private const val REQUEST_CODE_SET_TARGET = 3
        private const val KEY_POI_ITEM = "KEY_POI_ITEM"

        @JvmStatic
        fun start(context: Context, poiItem: PoiItem?) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(KEY_POI_ITEM, poiItem)
            context.startActivity(intent)
        }
    }

    override fun init(savedInstanceState: Bundle?, binding: ActivityMainBinding) {
        window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY) // 允许后台弹出
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED) // 允许覆盖锁屏
        binding.set.setOnClickListener { set() }
        binding.delete.setOnClickListener { delete() }
        binding.exit.setOnClickListener {
            ServiceUtils.stopService(ForegroundService::class.java)
            AppUtils.exitApp()
        }
        requestPermission()
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val poiItem = intent?.getParcelableExtra<PoiItem?>(KEY_POI_ITEM)
        poiItem?.let { RemindDialog.show(supportFragmentManager, it) }
    }

    private fun requestPermission() {
        PermissionUtil.request(this, REQUEST_CODE_PERMISSION, {
            refresh()
        }, {
            ToastUtils.showLong("请允许始终使用权限")
            ThreadUtils.runOnUiThreadDelayed({
                val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                intent.data = Uri.parse("package:$packageName")
                startActivityForResult(intent, REQUEST_CODE_APPLICATION_DETAILS_SETTINGS)
            }, 3000)
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            REQUEST_CODE_PERMISSION -> {
                PermissionUtil.onActivityResult(this, REQUEST_CODE_PERMISSION)
            }
            REQUEST_CODE_APPLICATION_DETAILS_SETTINGS -> {
                requestPermission()
            }
            REQUEST_CODE_SET_TARGET -> {
                if (resultCode == RESULT_OK) {
                    refresh()
                }
            }
        }
    }

    /**
     * 设置目的地
     */
    private fun set() {
        startActivityForResult(Intent(this, SetActivity::class.java), REQUEST_CODE_SET_TARGET)
    }

    /**
     * 删除目的地
     */
    private fun delete() {
        AwarenessUtil.deleteBarrier(
            this,
            { refresh() },
            { ToastUtils.showShort(it.message) })
    }

    /**
     * 获取目的地
     */
    private fun refresh() {
        AwarenessUtil.queryLocationBarrier(
            this,
            {
                binding.target.poiItem = it
                binding.target.root.visibility = if (it == null) View.GONE else View.VISIBLE
                ForegroundService.start(this, it)
            },
            { ToastUtils.showShort(it.message) })
    }

    /**
     * 按返回键返回桌面
     */
    override fun onBackPressed() {
        val intent = Intent(Intent.ACTION_MAIN)
        intent.addCategory(Intent.CATEGORY_HOME)
        startActivity(intent)
    }
}